Uec3.AcceptAddPartnerWindow = Ext.extend(Ext.app.Module, {
	id : 'acceptAddPartner',
	type : 'common/acceptAddPartner',

	/**
	 * Initialize this module. This function is called at startup (page
	 * load/refresh).
	 */
	init : function(initParams) {
	},
	lookUpMessage : function(id) {						   						   
		var desktop = Uec3.App.getDesktop();
		desktop.launchWindowByParam("simpleCompanyMessage", {
			        partnerId : '',
					id : id,
					isHiddenButton:true
				});
		
		 
},

	getComponents : function(params) {

		var acceptAddPartnerForm;
		var acceptAddPartner_ajax;
        var rejectForm;
        var reject_win;
		var componentAcceptAddPartnerInits = function() {

			acceptAddPartner_ajax = new Ext.data.Connection();
			
			function getText(companyId,name){
				
				var ralation = Ext.decode(params.jsArgs).relation;
				var relationName="买家和卖家";
				
				if(ralation=='0'){
					
					relationName="卖家"
					
				}else if(ralation=='1'){
					
					relationName="买家"
					
				}else{
					
					relationName="买家和卖家";	
				}
				var str = "<div style='font:normal 12px sans-serif'><a style='font-size:12px;' href='javascript:Ext.CUR_APP.getModule(\"acceptAddPartner\").lookUpMessage("
					+ companyId+");'>"
					+ name
					+"</a>公司,请求成为您的"
					+relationName+"</div>"
				
				return str;
			}

			// 评价evaluationForm
			rejectForm = new Ext.form.FormPanel({
						labelAlign : 'right',
						labelWidth : 60,
						frame : true,
						height : 250,
						url : '',
						items : [{							     
				        	 xtype:'label',
							 text:'请您输入拒绝原因:',
							 cls:'x-form-field',
							 anchor : '100%'
				               },{
				            	    height:120,
						        	hideLabel : true,
									name : 'remark',
									preventScrollbars : true,
									xtype : 'textarea',
									anchor : '100%',
									emptyText : '',
									maxLength:50,
									maxLengthText:"您的留言不能超过50个字符"
								}]
					});
		
			

			// 初始化window对象
			reject_win = new Ext.Window({
				layout : 'fit',
				closeAction : 'hide',
				width : 400,
				height : 250,
				title : '拒绝理由',
				collapsible : true,
				constrait : true,
				modal : true,
				draggable : true,
				plain : true,
				header : true,
				resizable : false,
				items : [rejectForm],
				buttons : [{
					text : '确定',
					handler : function() {

						if (!rejectForm.getForm().isValid()) {
							Ext.Msg.alert("提示", "请填写必须项");
							return;

						};
								var remark = rejectForm.getForm()
										.findField('remark').getValue();

								Ext.Ajax.request({
									url : Uec3.App.Constants.homeUrl+ '/notice/revert.do',
									success : function(res) {
                                      var result = Ext
										.decode(res.responseText);
									if (result.success == true) {

										reject_win.hide();
									    acceptAddPartnerForm.ownerCt.close();
									}else{
										
										if(result.failure=="isSelf"){
				                        	 
				                     		Ext.Msg
											.alert("提示",
													"您好,不能加自己为客户");                      	 
				                         }
									 else if(result.failure=="alreadyPartner"){
							        	   
							         		Ext.Msg
											.alert("提示",
													" 您好,该公司已经是你的客户,不能重复增加,请到客户列表查看");   
							        	   
							           }else{
									
									
									       Ext.Msg.alert("提示", result.failure);
									
							           }	
										
									}
								},
									failure : function(res) {
										
										Ext.Msg.alert("提示", '网络异常,请重先请求');

									},
                            
									params : {
										remark : remark,noticeId: params.id
									}

								});

					}
				}, {
					text : '取消',

					handler : function() {

					   reject_win.hide();

					}

				}],

				buttonAlign : 'center'

			});
			
			
			

			var partner_ds = new Ext.data.Store({

						proxy : new Ext.data.HttpProxy({
									url : Uec3.App.Constants.homeUrl
											+ '/partner/query_partner!query_condition.do'
								}),
						baseParams : {
							start : 0,
							limit : 20,
							companyId:Ext.decode(params.jsArgs).inviteCompanyId
						},
						pruneModifiedRecords : true,
						reader : new Ext.data.JsonReader({
									totalProperty : 'totalProperty',
									root : 'root'
								}, [{
											name : 'id',
											type : 'int'
										}, {
											name : 'companyId',
											type : 'int'
										}, {
											name : 'ownerId'
										}, {
											name : 'areaId',
											type : 'int'
										}, {
											name : 'name'
										}, {

											name : 'taxCode'
										}, {

											name : 'pyCode'
										}, {
											name : 'contactor'
										}, {
											name : 'contactorIdentityCode'
										}, {
											name : 'code'
										}, {
											name : 'simpleName'
										}, {
											name : 'nameEn'
										}, {
											name : 'business'
										},{
											name : 'address'
										}, {
											name : 'postcode'
										}, {
											name : 'tel'
										}, {
											name : 'fax'
										}, {
											name : 'invoicAddress'
										}, {
											name : 'bankName'
										}, {
											name : 'email'
										}, {
											name : 'bankCode'
										}, {
											name : 'qq'
										}, {
											name : 'msn'
										}, {
											name : 'web'
										}, {
											name : 'discount',
											type : 'float'
										}, {
											name : 'relation'
										}, {
											name : 'unpaid',
											type : 'float'
										}, {
											name : 'paid',
											type : 'float'
										}, {
											name : 'unreceived',
											type : 'float'
										}, {
											name : 'received',
											type : 'float'
										}, 
										{
											name : 'gmtCreate',
											type : 'string'
										}, {
											name : 'gmtModify',
											type : 'string'
										}, {
											name : 'isDelete',
											type : 'boolean'
										}

								]),
						remoteSort : true,

						// 默认排序字段
						sortInfo : {
							field : "id",
							direction : "asc"
						}

					});
			
			
			// 初始化notice_grid表格对象
			var sm = new Ext.grid.CheckboxSelectionModel({
						moveEditorOnEnter : true,
						singleSelect : true
					});
			
			
			var partner_colModel = new Ext.grid.ColumnModel([
			                             					sm,{
			                             						header : "客户名称",
			                             						width : 150,
			                             						sortable : true,
			                             						dataIndex : 'name'
			                             					}, {
			                             						header : "税号",
			                             						sortable : true,
			                             						width : 150,
			                             						dataIndex : 'taxCode'
			                             					},{
			                             						id:"contactorIdentityCode",
			                             						header : "法人省份证号",
			                             						width : 150,
			                             						dataIndex : 'contactorIdentityCode'
			                             					}]);
			
			partner_grid = new Ext.grid.GridPanel(

					{
						// title : '往来单位列表',
						region : 'center',
						iconCls : 'icon-grid',
						
						enableColumnMove : false,
						enableColumnMoveResize : false,
						height:225,
						margins : '3 3 3 0',
						defaults : {
							autoScroll : true
						},
						stripeRows : true,
						autoExpandColumn : 'contactorIdentityCode',
						loadMask : {
							msg : "...加载数据中"
						},
						store : partner_ds,
					    cm : partner_colModel,               
						sm : sm ,
						viewConfig : {
							//forceFit :true,
							columnsText : '显示的列',
							scrollOffset : 20,
							sortAscText : '升序',
							sortDescText : '降序'
						}
					});
			
			
			var partnerPanel = new Ext.Panel({
				height : 125,
				layout : 'fit',
				title:"根据请求公司的税号和法人身份证号查找往来单位列表中是否有匹配的信息",
				items : [partner_grid]

			});
			
		
			
			partner_ds.load({
							callback: function(records,options,success){		
							         if(success){			        	 
							        	      if(!records.length){
										        	 //Ext.Msg.alert('提示',"不存在和请求公司相匹配的客户信息");
							        	    	  acceptAddPartnerForm.setHeight(250);
							        	    	  acceptAddPartnerForm.ownerCt.setHeight(250);
							        	    	  partnerPanel.hide();
							        	      }else{						        	    	  
							        	    	  sm.selectFirstRow(); 						        	    	  
							        	      }

							         }else{
							        	 
							        	 
							        	 Ext.Msg.alert('提示',"网络连接异常...请稍候再试");
							         }			  
								
							 }		
						
						});
			acceptAddPartnerForm = new Ext.form.FormPanel({
				labelAlign : 'right',
				frame : true,
				labelWidth :65,
				height : 375,
				loadingText : '加载数据中...',
				buttonAlign : 'right',
				layout : "form",
				items : [{
				
							xtype : 'panel',
							height:20,
							html: getText(Ext.decode(params.jsArgs).inviteCompanyId,Ext.decode(params.jsArgs).inviteCompanyName)
						},{							     
				        	 xtype:'label',
							 text:'对方留言:',
							 height:10,
							 cls:'x-form-field',
							 anchor : '100%'
								 
				        },{
				
									   // grow : true,
							        	height:120,
							        	hideLabel : true,
										name : 'reason',
										preventScrollbars : true,
										value:Ext.decode(params.jsArgs).reason,
										readOnly:true,
										xtype : 'textarea',
										style:"color:#aaaaaa;",
										anchor : '100%',
										emptyText : '',
										maxLength:50,
										maxLengthText:"您的留言不能超过50个字符"
								
						},partnerPanel

				],
				buttons : [{
					text : '同意',
					handler : function() {
						if (!acceptAddPartnerForm.getForm().isValid()) {
							Ext.Msg.alert("提示", "请填写必须项");
							return;

						}
						if (params.companyId == 0 || params.companyId== "") {
							Ext.Msg.alert("提示",
									"请先点击检查认证按钮,确认对方已是认证公司,才能进行这步操作")

							return;
						}
					
						var selectBean = sm.getSelected();
						var ownerPartnerId=null;
						if(selectBean){
						   
							ownerPartnerId=selectBean.get("id");
						
						}
						acceptAddPartner_ajax.request({
							url : Uec3.App.Constants.homeUrl+ '/notice/deal.do',
							success : function(res) {

								var result = Ext
										.decode(res.responseText);
								if (result.success == true) {

									Ext.Msg.alert("提示", "增加往来单位成功",function(){
										
										acceptAddPartnerForm.ownerCt.close();
									
									});

								} else {
									
									 if(result.failure=="isSelf"){		                        	 
				                     		Ext.Msg.alert("提示","您好,不能加自己为客户");                      	 
				                         }
									 else if(result.failure=="alreadyPartner"){
							        	   
							         		Ext.Msg.alert("提示","您好,该公司已经是你的客户,不能重复增加,请到客户列表查看");   
							        	   
							           }
							         else if(result.failure=="you are must choose one"){
							         
							         	Ext.Msg.alert("提示","请根据税号选择一条往来单位信息");   
							         	
							         }  
							         else{									
									
									Ext.Msg.alert("提示", result.failure);
									
							        }
								}
							},

							failure : function(res) {

								Ext.Msg.alert('失败', "网络出现异常,请重先连接");

							},

							params : {

								noticeId: params.id,ownerPartnerId:ownerPartnerId
							}

						});

					}
				}, {
					text : '拒绝',

					handler : function() {

					reject_win.show();

					},

					style : "margin-left:20px"

				},{
					text : '取消',

					handler : function() {

					acceptAddPartnerForm.ownerCt.close();

					},

					style : "margin-left:20px"

				}]
			});
			

		};

		var controlAcceptAddPartnerFormEvent = function() {

			acceptAddPartner_ajax.on('beforerequest', function() {Ext.MessageBox.wait("更新数据中...", "提示");});

			acceptAddPartner_ajax.on('requestcomplete', function() {Ext.MessageBox.hide();});

		};

		componentAcceptAddPartnerInits();

		controlAcceptAddPartnerFormEvent();

		return acceptAddPartnerForm;

	},

	/**
	 * Create this modules window here.
	 */
	createWindow : function(params) {
		var windowId = 'requestAddPartner';
		var desktop = this.app.getDesktop();
		if (params) {
			windowId += "_" + params.winId;
		}
		
		var win = desktop.getWindow(windowId);
		if (!win) {
			win = desktop.createWindow({
						id : windowId,
						title : '处理往来单位消息',
						width : 475,
						height : 375,
						//autoScroll : true,
						iconCls : 'acc-icon',
						shim : false,
						animCollapse : false,
						constrainHeader : true,
						resizable : false,
						maximizable : false,
						taskbuttonTooltip : "处理往来单位消息",
						layout : "fit",
						tools : [{
									id : 'refresh',
									handler : Ext.emptyFn,
									scope : this
								}],
						items : [this.getComponents(params)]
					});

		}
		win.show();

	}
});